package dasom;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SungJukV1 extends JFrame {

	private JPanel contentPane;
	private JTextField name;
	private JTextField kor;
	private JTextField eng;
	private JTextField math;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SungJukV1 frame = new SungJukV1();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SungJukV1() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("::성적 처리프로그램 v1::");
		
		JLabel lblName = new JLabel("이 름");
		
		name = new JTextField();
		name.setColumns(10);
		
		JLabel lblKor = new JLabel("국 어");
		
		kor = new JTextField();
		kor.setColumns(10);
		
		JLabel lblEng = new JLabel("영 어");
		
		eng = new JTextField();
		eng.setColumns(10);
		
		JLabel lblMath = new JLabel("수 학");
		
		math = new JTextField();
		math.setColumns(10);
		
		JButton saveBtn = new JButton("입 력 완 료");
		saveBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//비지니스 로직 처리(총점,평균,학점 계산)
				int tot=Integer.parseInt(kor.getText())+Integer.parseInt(eng.getText())+Integer.parseInt(math.getText());
				float avg=(float)tot/3;
				char grd='가';
				switch((int)avg/10){
				case 10:case 9:grd='수';break;
				case 8:        grd='우';break;
				case 7:        grd='미';break;
				case 6:        grd='양';
				}
				
				
				//프리젠테이션 로직 처리
				StringBuilder result=new StringBuilder();
				result.append("이름:").append(name.getText()).append("\n");
				result.append("국어:").append(kor.getText()).append("\n");
				result.append("영어:").append(eng.getText()).append("\n");
				result.append("수학:").append(math.getText()).append("\n");
				result.append("총점:").append(tot).append("\n");
				result.append("평균:").append(String.format("%3.2f", avg)).append("\n");
				result.append("학점:").append(grd);
				
				JOptionPane.showMessageDialog(null, result.toString());
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(94)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblName)
								.addComponent(lblKor)
								.addComponent(lblEng)
								.addComponent(lblMath))
							.addGap(73)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(math, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(eng, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(kor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(138)
							.addComponent(lblNewLabel))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(160)
							.addComponent(saveBtn)))
					.addContainerGap(113, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel)
					.addGap(30)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblName)
						.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblKor)
						.addComponent(kor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblEng)
						.addComponent(eng, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblMath)
						.addComponent(math, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
					.addComponent(saveBtn)
					.addContainerGap())
		);
		contentPane.setLayout(gl_contentPane);
	}
}
